<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS盒子模型实例</title>
    <style>
        /* 全局样式 */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            padding: 20px;
            color: #333;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 20px;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        
        h2 {
            color: #2980b9;
            margin: 20px 0 10px;
        }
        
        .box-model-explanation {
            display: flex;
            justify-content: space-between;
            margin-bottom: 30px;
        }
        
        .explanation-text {
            flex: 1;
            padding-right: 20px;
        }
        
        .box-visual {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .box-diagram {
            position: relative;
            width: 200px;
            height: 200px;
            background-color: #e74c3c;
            border: 10px solid #f39c12;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
        }
        
        .box-diagram::before {
            content: "";
            position: absolute;
            top: -40px;
            left: -40px;
            right: -40px;
            bottom: -40px;
            border: 10px dashed #3498db;
            z-index: -1;
        }
        
        .margin-label, .border-label, .padding-label, .content-label {
            position: absolute;
            background-color: rgba(0,0,0,0.7);
            color: white;
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 12px;
        }
        
        .margin-label {
            top: -50px;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .border-label {
            top: -25px;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .padding-label {
            top: 10px;
            left: -45px;
        }
        
        .content-label {
            bottom: 10px;
            right: 10px;
        }
        
        .examples {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-top: 20px;
        }
        
        .example-box {
            flex: 1;
            min-width: 300px;
            padding: 15px;
            border-radius: 5px;
            background-color: #f9f9f9;
            border-left: 4px solid #3498db;
        }
        
        .box {
            width: 200px;
            height: 100px;
            margin: 20px auto;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
        }
        
        .standard-box {
            background-color: #3498db;
            padding: 20px;
            border: 5px solid #2980b9;
            margin: 10px;
        }
        
        .border-box {
            background-color: #e74c3c;
            padding: 20px;
            border: 5px solid #c0392b;
            margin: 10px;
            box-sizing: border-box;
        }
        
        .code-block {
            background-color: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 5px;
            font-family: 'Courier New', monospace;
            margin: 10px 0;
            overflow-x: auto;
            white-space: pre-wrap;
        }
        
        .property-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        
        .property-table th, .property-table td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }
        
        .property-table th {
            background-color: #3498db;
            color: white;
        }
        
        .property-table tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        
        .comparison {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }
        
        .comparison-item {
            flex: 1;
            padding: 15px;
            text-align: center;
        }
        
        .comparison-box {
            width: 150px;
            height: 150px;
            margin: 0 auto 15px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
        }
        
        .content-box-example {
            background-color: #9b59b6;
            padding: 20px;
            border: 10px solid #8e44ad;
            box-sizing: content-box;
        }
        
        .border-box-example {
            background-color: #1abc9c;
            padding: 20px;
            border: 10px solid #16a085;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CSS盒子模型详解</h1>
        
        <div class="box-model-explanation">
            <div class="explanation-text">
                <h2>什么是CSS盒子模型？</h2>
                <p>CSS盒子模型是网页设计的基础概念，每个HTML元素都被视为一个矩形盒子，由以下部分组成：</p>
                <ul>
                    <li><strong>内容(Content)</strong> - 显示文本、图像等实际内容</li>
                    <li><strong>内边距(Padding)</strong> - 内容与边框之间的透明区域</li>
                    <li><strong>边框(Border)</strong> - 围绕内容和内边距的边界</li>
                    <li><strong>外边距(Margin)</strong> - 盒子与其他元素之间的透明区域</li>
                </ul>
                
                <h2>盒子模型类型</h2>
                <p>CSS中有两种盒子模型：</p>
                <ul>
                    <li><strong>标准盒子模型(content-box)</strong> - 宽度/高度只包括内容区域</li>
                    <li><strong>替代盒子模型(border-box)</strong> - 宽度/高度包括内容、内边距和边框</li>
                </ul>
            </div>
            
            <div class="box-visual">
                <div class="box-diagram">
                    内容区域
                    <div class="margin-label">外边距(Margin)</div>
                    <div class="border-label">边框(Border)</div>
                    <div class="padding-label">内边距(Padding)</div>
                    <div class="content-label">内容(Content)</div>
                </div>
            </div>
        </div>
        
        <h2>盒子模型属性</h2>
        <table class="property-table">
            <tr>
                <th>属性</th>
                <th>描述</th>
                <th>示例值</th>
            </tr>
            <tr>
                <td>width / height</td>
                <td>设置内容区域的宽度和高度</td>
                <td>width: 200px; height: 100px;</td>
            </tr>
            <tr>
                <td>padding</td>
                <td>设置内容与边框之间的空间</td>
                <td>padding: 10px; (四个方向相同)</td>
            </tr>
            <tr>
                <td>border</td>
                <td>设置边框的样式、宽度和颜色</td>
                <td>border: 2px solid #000;</td>
            </tr>
            <tr>
                <td>margin</td>
                <td>设置元素与其他元素之间的空间</td>
                <td>margin: 20px auto; (上下20px，左右自动)</td>
            </tr>
            <tr>
                <td>box-sizing</td>
                <td>定义如何计算元素的宽度和高度</td>
                <td>box-sizing: border-box;</td>
            </tr>
        </table>
        
        <h2>盒子模型实例</h2>
        <div class="examples">
            <div class="example-box">
                <h3>标准盒子模型 (content-box)</h3>
                <p>这是默认的盒子模型，元素的宽度和高度只包括内容区域。</p>
                <div class="code-block">
.standard-box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid #2980b9;
    margin: 10px;
    background-color: #3498db;
    box-sizing: content-box; /* 默认值 */
}
                </div>
                <div class="box standard-box">
                    总宽度: 200 + 40 + 10 = 250px
                </div>
                <p>总宽度 = 内容宽度(200px) + 左右内边距(40px) + 左右边框(10px) = 250px</p>
            </div>
            
            <div class="example-box">
                <h3>替代盒子模型 (border-box)</h3>
                <p>元素的宽度和高度包括内容、内边距和边框。</p>
                <div class="code-block">
.border-box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid #c0392b;
    margin: 10px;
    background-color: #e74c3c;
    box-sizing: border-box;
}
                </div>
                <div class="box border-box">
                    总宽度: 200px
                </div>
                <p>总宽度 = 指定的宽度(200px)，包括内容、内边距和边框</p>
            </div>
        </div>
        
        <h2>两种盒子模型对比</h2>
        <div class="comparison">
            <div class="comparison-item">
                <h3>标准盒子模型 (content-box)</h3>
                <div class="comparison-box content-box-example">
                    内容区域
                </div>
                <p>设置宽度为150px，但实际占用更多空间</p>
                <p>实际宽度 = 150px + 40px(内边距) + 20px(边框) = 210px</p>
            </div>
            
            <div class="comparison-item">
                <h3>替代盒子模型 (border-box)</h3>
                <div class="comparison-box border-box-example">
                    内容区域
                </div>
                <p>设置宽度为150px，实际占用空间就是150px</p>
                <p>实际宽度 = 150px (包括内边距和边框)</p>
            </div>
        </div>
    </div>
</body>
</html>